package com.yihuan.controller;

import com.yihuan.entity.CodeEntity;
import com.yihuan.entity.CompanyEntity;
import com.yihuan.entity.SellerEntity;
import com.yihuan.global.MessageConstant;
import com.yihuan.service.CodeService;
import com.yihuan.service.CompanyService;
import com.yihuan.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:梦醒
 * Date: 2022/4/19 15:26
 * Dream:
 * 信言不美，美言不信。
 * 善者不辩，辩者不善。
 * 知者不博，博者不知。
 * Function:
 */
@RestController
public class CompanyController {
    int pageNumber = 0;
    int count = 0;
    //使用map存储数据
    private Map<String, Object> map = new HashMap<>();
    //数据页数和起始值
    private int num;
    @Autowired
    CompanyService companyService;
    @Autowired
    SellerService sellerService;
    @Autowired
    CodeService codeService;
    
    /**
     * 以商铺名称来查询
     *
     * @param companyEntity
     * @param session
     * @return
     */
    @RequestMapping("/findCompanyByCompName")
    public Map<String, Object> findCompanyByCompName(@RequestBody CompanyEntity companyEntity, HttpSession session) {
        //查询医院状态码
        List<CompanyEntity> findByHospitalCode = companyService.findCompanyByCompName(companyEntity);
        //用户医院
        if (findByHospitalCode.size() == 1) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain", MessageConstant.COMPANY_SELECT_NAME_SUCCESS);
            return map;
        }
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain", MessageConstant.COMPANY_SELECT_NAME_FALL);
        return map;
    }
    
    /**
     * 登记公司信息
     *
     * @param file
     * @param CountryID
     * @param compName_1
     * @param compAddress_1
     * @param compPhone_1
     * @param compProfile_1
     * @param compEmail_1
     * @param session
     * @return
     */
    @RequestMapping("/insertCompanyByCompanyData")
    public Map<String, Object> insertCompanyByCompanyData(@RequestParam MultipartFile file,
                                                          int CountryID,
                                                          String compName_1,
                                                          String compAddress_1,
                                                          String compPhone_1,
                                                          String compProfile_1,
                                                          String compEmail_1,
                                                          @RequestParam MultipartFile compPhoto,
                                                          HttpSession session) {
        
        CompanyEntity companyEntity = new CompanyEntity();
        if (!file.isEmpty() && !compPhoto.isEmpty()) {
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            String compPhotoFileName = System.currentTimeMillis() + compPhoto.getOriginalFilename();
            String savePath = MessageConstant.file;
            File dest = new File(savePath + File.separator + fileName);
            File dest1 = new File(savePath + File.separator + compPhotoFileName);
            try {
                file.transferTo(dest);
                compPhoto.transferTo(dest1);
                //执照
                companyEntity.setCompBusinessLicense(fileName);
                //删除状态
                companyEntity.setCompState(2);
                //地址
                companyEntity.setCompAddress(compAddress_1);
                //城区编号
                companyEntity.setCountryID(CountryID);
                //名称
                companyEntity.setCompName(compName_1);
                //电话
                companyEntity.setCompPhone(compPhone_1);
                //简介
                companyEntity.setCompProfile(compProfile_1);
                //电子邮件
                companyEntity.setCompEmail(compEmail_1);
                //公司图片
                companyEntity.setCompPhoto(compPhotoFileName);
                //设置时间的格式
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //获取时间的方法
                Date d = new Date();
                //将获取的时间转换成设置的时间格式进行存储
                //登记时间
                companyEntity.setCompDate(sf.format(d));
                //登记医院信息
                int insertByHospitalMessage = companyService.insertCompanyByCompanyData(companyEntity);
                //登记医院信息通过
                if (insertByHospitalMessage == 1) {
                    //获取公司编号
                    List<CompanyEntity> findCompanyByCompNameAndCompState = companyService.findCompanyByCompNameAndCompState(companyEntity);
                    //给用户登记公司编号
                    SellerEntity sellerEntity = new SellerEntity();
                    sellerEntity.setCompID(findCompanyByCompNameAndCompState.get(0).getCompID());
                    sellerEntity.setSellerLoginName((String) session.getAttribute("SellerLoginName"));
                    sellerService.updateSellerByCompID(sellerEntity);
                    map.put("code", MessageConstant.SUCCESS_CODE);
                    map.put("explain", MessageConstant.COMPANY_INSERT_SUCCESS);
                    return map;
                } else {
                    map.put("code", MessageConstant.FALL_CODE);
                    map.put("explain", MessageConstant.COMPANY_INSERT_FALL);
                }
            } catch (IOException e) {
                e.printStackTrace();
                map.put("code", MessageConstant.FALL_CODE);
                map.put("explain", MessageConstant.COMPANY_INSERT_FALL);
            }
        } else if (file.isEmpty()) {
            map.put("code", MessageConstant.HALF_CODE);
            map.put("explain ", MessageConstant.COMPANY_INSERT_HALF_FALL);
        }
        return map;
    }
    
    
    /**
     * 分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findCompanyByCompanyList")
    public Map<String, Object> findCompanyByCompanyList(@RequestBody CompanyEntity model, HttpSession session) {
        List<CompanyEntity> findCompanyByCompanyList = companyService.findCompanyByCompanyList(model);
        if (findCompanyByCompanyList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.COMPANY_LIMIT_FALL);
            return map;
        }
        pageNumber = countCompanyList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("company_List", findCompanyByCompanyList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.COMPANY_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countCompanyList(CompanyEntity model) {
        count = companyService.countCompanyList();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 模糊分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findCompanyByCompanyConcatList")
    public Map<String, Object> findCompanyByCompanyConcatList(@RequestBody CompanyEntity model, HttpSession session) {
        List<CompanyEntity> findCompanyByCompanyConcatList = companyService.findCompanyByCompanyConcatList(model);
        if (findCompanyByCompanyConcatList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.COMPANY_LIMIT_Concat_FALL);
            return map;
        }
        pageNumber = countCompanyConcatList(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("company_List", findCompanyByCompanyConcatList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.COMPANY_LIMIT_Concat_SUCCESS);
        return map;
    }
    
    /**
     * 统计模糊分页查询数据
     *
     * @param model
     * @return
     */
    public int countCompanyConcatList(CompanyEntity model) {
        count = companyService.countCompanyConcatList(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 更改商户信息状态
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateCompanyByCompState")
    public Map<String, Object> updateCompanyByCompState(@RequestBody CompanyEntity model, HttpSession session) {
        int updateCompanyByCompState = companyService.updateCompanyByCompState(model);
        //判断数据是否存在
        if (updateCompanyByCompState == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.COMPANY_STATE_FALL);
            return map;
        }
        //账户登录成功
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("explain ", MessageConstant.COMPANY_STATE_SUCCESS);
        return map;
    }
    
    /**
     * 注册密钥
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/updateCompanyByCompStateAndCode")
    public Map<String, Object> updateCompanyByCompStateAndCode(@RequestBody CompanyEntity model, HttpSession session) {
        Object compID = session.getAttribute("compID");
        CodeEntity codeEntity = new CodeEntity();
        codeEntity.setCodeName(model.getCompCode());
        codeEntity.setCompID((Integer) compID);
        List<CodeEntity> findCodeByCodeID = codeService.findCodeByCodeID(codeEntity);
        if (findCodeByCodeID.size() == 1) {
            model.setCompID((Integer) compID);
            model.setCompState(1);
            int updateCompanyByCompStateAndCode = companyService.updateCompanyByCompStateAndCode(model);
            //判断数据是否存在
            if (updateCompanyByCompStateAndCode == 0) {
                map.put("code", MessageConstant.FALL_CODE);
                map.put("explain ", MessageConstant.COMPANY_CODE_FALL);
                return map;
            } else {
                //密钥登记成功
                map.put("code", MessageConstant.SUCCESS_CODE);
                map.put("explain ", MessageConstant.COMPANY_CODE_SUCCESS);
                return map;
            }
        } else {
            //账户登录成功
            map.put("code", MessageConstant.HALF_CODE);
            map.put("explain ", MessageConstant.COMPANY_CODE_HALF);
            return map;
        }
    }
    
    /**
     * 前端分页查询数据
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findCompanyByCompanyListAndCompState")
    public Map<String, Object> findCompanyByCompanyListAndCompState(@RequestBody CompanyEntity model, HttpSession session) {
        List<CompanyEntity> findCompanyByCompanyList = companyService.findCompanyByCompanyListAndCompState(model);
        if (findCompanyByCompanyList.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.COMPANY_WEB_LIMIT_FALL);
            return map;
        }
        pageNumber = countCompanyListAndCompState(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("company_List", findCompanyByCompanyList);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.COMPANY_WEB_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据
     *
     * @param model
     * @return
     */
    public int countCompanyListAndCompState(CompanyEntity model) {
        count = companyService.countCompanyListAndCompState();
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
    
    /**
     * 前端分页查询数据 按地址
     *
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/findCompanyByCompanyListAndCountryID")
    public Map<String, Object> findCompanyByCompanyListAndCountryID(@RequestBody CompanyEntity model, HttpSession session) {
        List<CompanyEntity> findCompanyByCompanyListAndCountryID = companyService.findCompanyByCompanyListAndCountryID(model);
        if (findCompanyByCompanyListAndCountryID.size() == 0) {
            map.put("code", MessageConstant.FALL_CODE);
            map.put("explain ", MessageConstant.COMPANY_WEB_ADDRESS_LIMIT_FALL);
            return map;
        }
        pageNumber = countCompanyListAndCountryID(model);
        map.put("code", MessageConstant.SUCCESS_CODE);
        map.put("company_List", findCompanyByCompanyListAndCountryID);
        map.put("pageNumber", pageNumber);
        map.put("count", count);
        map.put("explain ", MessageConstant.COMPANY_WEB_ADDRESS_LIMIT_SUCCESS);
        return map;
    }
    
    /**
     * 前端统计分页查询数据 按地址
     *
     * @param model
     * @return
     */
    public int countCompanyListAndCountryID(CompanyEntity model) {
        count = companyService.countCompanyListAndCountryID(model);
        if (count % model.getNumber() == 0) {
            pageNumber = count / model.getNumber();
        } else {
            pageNumber = count / model.getNumber() + 1;
        }
        return pageNumber;
    }
}
